const areaData = require('../../../data/area')
import api from '../../../api/api'
import Toast from 'tdesign-miniprogram/toast/index';
import {addressadd, addressedit} from '../../../api/conf'

Page({
  data: {
    isUpdate: false,
    addressId: 0,
    name: '',
    phone: '',
    areasShow: '',
    areas: '',
    detail: '',
    provinceName: [],
    provinceSelIndex: 0,
    cityName: [],
    citySelIndex: 0,
    districtName: [],
    districtSelIndex: 0,
    showDistpicker: false,
  },
  onLoad(options) {
    // 初始化数据
    if (options.item) {
      let item = JSON.parse(options.item);
      this.setData({
        addressId: item.Id,
        isUpdate: !!item.Id,
        name: item.Name,
        phone: item.Phone,
        detail: item.Address,
        areasShow: item.ProvinceName + "-" + item.CityName + "-" + item.AreaName,
        areas: item.ProvinceName  + item.CityName  + item.AreaName
      })
      this.setAreaData(item.ProvinceName, item.CityName, item.AreaName)
    }else{
      this.setAreaData()
    }

  },

  showDistpicker: function() {
    this.setData({
      showDistpicker: true
    })
  },

  distpickerCancel: function() {
    this.setData({
      showDistpicker: false
    })
  },

  distpickerSure: function() {
    const province = this.data.provinceName[this.data.provinceSelIndex]
    const city = this.data.cityName[this.data.citySelIndex]
    const district = this.data.districtName[this.data.districtSelIndex]
    const areasShow = province + "-" + city + "-" + district
    const areas = province  + city  + district
    this.setData({areas, areasShow})
    this.distpickerCancel()
  },

  changeArea: function(e) {
    this.setData({
      provinceSelIndex: e.detail.value[0],
      citySelIndex: e.detail.value[1],
      districtSelIndex: e.detail.value[2]
    })
    this.setAreaData()
  },

  setAreaData: function(p= '', c = '', a = '') {
    let { provinceSelIndex } = this.data
    let { citySelIndex } = this.data
    let { districtSelIndex } = this.data

    // 设置省的数据
    const province = areaData['100000'];
    const provinceName = [];
    const provinceCode = [];

    for (const item in province) {
      provinceName.push(province[item])
      provinceCode.push(item)
    }

    this.setData({
      provinceName: provinceName,
    })

    if(p){
      provinceSelIndex = provinceName.indexOf(p)
      this.setData({
        provinceSelIndex
      })
    }

    // 设置市的数据
    const city = areaData[provinceCode[provinceSelIndex]];
    const cityName = [];
    const cityCode = [];
    for (const item in city) {
      cityName.push(city[item])
      cityCode.push(item)
    }
    this.setData({
      cityName: cityName,
    })

    if(c){
      citySelIndex = cityName.indexOf(c)
      this.setData({
        citySelIndex
      })
    }

    // 设置区的数据
    const district = areaData[cityCode[citySelIndex]];
    const districtName = [];
    for (const item in district) {
      districtName.push(district[item])
    }
    this.setData({
      districtName: districtName,
    })

    if(a){
      districtSelIndex = districtName.indexOf(a)
      this.setData({
        districtSelIndex
      })
    }
  },
  savePersonInfo: function(e) {
    let formData = e.detail.value
    let telRule = /^1[3-9]\d{9}$/,
      nameRule =   /^[\u4e00-\u9fa5a-zA-Z0-9]+$/
    if(!nameRule.test(formData.name)){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '收货人姓名只能为中文',
      });
      return;
    }
    if(!formData.areas){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入所在地区',
      });
      return;
    }
    if(!formData.name){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入收货人姓名',
      });
      return;
    }
    if(!formData.phone){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入手机号码',
      });
      return;
    }
    if (!telRule.test(formData.phone)) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '手机号码格式不正确',
      });
      return;
    }
    if(!formData.detail){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请输入详细地址',
      });
      return;
    }
    this.setData({
      name: formData.name,
      phone: formData.phone,
      detail: formData.detail,
    })
    let postData = {
      Name: this.data.name,
      Phone: this.data.phone,
      ProvinceName: this.data.provinceName[this.data.provinceSelIndex],
      CityName: this.data.cityName[this.data.citySelIndex],
      AreaName: this.data.districtName[this.data.districtSelIndex],
      Address: this.data.detail
    }
    let url
    if(this.data.isUpdate){
      url =  addressedit + '?addressId=' + this.data.addressId
    }else{
      url = addressadd
    }
    api.post(url , postData).then((res) => {
        if (res.Code === 200) {
          let pages = getCurrentPages();
          let prevPage = pages[pages.length -2]
          prevPage.getAddressList()
          wx.navigateBack({delta: 1})
        }else{
          const errorMsg = res.Content
          Toast({
            context: this,
            selector: '#t-toast',
            message: errorMsg,
          });
        }
      })
  },
  inputName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  inputPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputAreas: function(e) {
    this.setData({
      areas: e.detail.value
    })
  },
  inputDetail: function (e) {
    this.setData({
      detail: e.detail.value
    })
  }
})